import XMLList from "./XMLList";
import { Parser } from "./parser";
import { Node, NodeAttribute } from "./node";

/**
 * XML文件
 * @author clong 2020.12.11
 */
export default class XML{

    public name:string = "";
    public text:string = "";
    public attributes:Array<string> = [];
    private $attrValMap:Object = {};
    public elements:XMLList;

    constructor( xmlStr: string="" ) {
        
        this.parseXML( xmlStr );
    }

    public parseXML( xmlStr:string ):void{

        // console.log(xml);

        this.clear();

        if(!xmlStr){
            return;
        }

        let nodes:Array<Node> = Parser.fromXml(xmlStr);
        let node:Node;
        for(let i=0;i<nodes.length;i++){
            node = nodes[i];
            if( node._type == "element" ){
                this.parseNode( node );                
            }
        }
    }

    public parseNode( node:Node ):void{

        if( node ){
            this.name = node._value;    
            let i = 0;
            let nodeAttr:NodeAttribute = null;
            for(i=0;i<node._attr.length;i++){
                nodeAttr = node._attr[i];
                this.attributes.push( nodeAttr._name );
                this.setAttrValue( nodeAttr._name , nodeAttr._value );
            }        
            if( node._children && node._children.length > 0 ){
                let child:Node = null;
                for(let i=0;i<node._children.length;i++){
                    child = node._children[i];
                    if( child._type == "element" ){
                        this.elements.addNode( child );
                    }                    
                }                
            }
        }        
    }

    public setAttrValue( name:string , value:any ):void{

        this.$attrValMap[name] = value;
        this[name] = value;
    }

    public getAttrValue( name:string ):any{

        return this[ name ];
    }

    public toJson():Object{

        let obj:any = {"children":[]};
        let key:any;
        for( key in this.$attrValMap ){
            if( key == "children" ) continue;//保留字段名
            obj[ key ] = this.$attrValMap[key];            
        }
        if( this.elements.Count > 0 ){
            for( let i=0;i<this.elements.Count;i++ ){
                let xml:XML = this.elements.get_Item(i);
                if( xml ){
                    obj.children.push(xml.toJson());
                }
            }
        }
        return obj;
    }

    public clear():void{

        while( this.$attrValMap && Object.keys(this.$attrValMap).length > 0 ){
            let key:any;
            for( key in this.$attrValMap ){
                delete this.$attrValMap[key];
            }
        }

        this.elements = new XMLList();
        this.$attrValMap = {};
        this.attributes = [];
    }

    // public parseXML( xmlStr:string ):void{

        // let uselines:Array<string> = [];//有用的行
        // let lines:Array<string> = xml.split("\r\n");
        // let isFindNote:boolean = false;//是否正在查找注释
        // while( lines && lines.length > 0 ){
        //     let line:string = lines.shift();
        //     line = this.formatOneLine(line);//格式化每一行
        //     if( line.indexOf("<?xml") == 0 ){//第一行声明
        //         //获取版本号信息等
        //         continue;
        //     }
        //     if(!line){
        //         continue;//空白行无信息
        //     }
        //     //删除所有的注释
        //     if( line.indexOf("<!--") == 0 ){
        //         isFindNote = line.indexOf("-->") == 0;//如果找到注释尾行，则继续
        //         continue;            
        //     }
        //     if( isFindNote){
        //         if( line.indexOf("-->") == 0  ){
        //             isFindNote = false;
        //         }
        //         continue;
        //     }
        //     uselines.push( line );
        // }
    // }

    /**移除开头的空白字符,并格式化该行 */
    // private formatOneLine( str:string ):string{

    //     let i = 0;
    //     let arr:Array<string> = str.split('');
    //     while( arr && arr.length > 0 ){
    //         if( arr[0] == " " || arr[0] == "\br" ){
    //             arr.shift();//去除开头的空白
    //             continue;
    //         }else if( arr[i] == " " && arr[i+1] == " " ){
    //             arr.splice(i,1); //保证每个属性之间只有一个空格
    //             continue;
    //         }
    //         i++;
    //         if( i == str.length ){
    //             break;
    //         }
    //     }
    //     return str;
    // }
}